package com.sk.waste.app.activitys

import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lc.smc.lib.annotations.LCBindView
import com.lc.smc.lib.interfaces.LCInterface
import com.lc.smc.lib.utils.LCSPUtil
import com.sk.waste.app.R
import com.sk.waste.app.adapters.InterestAdapter
import com.sk.waste.app.adapters.TenderAdapter
import com.sk.waste.app.bases.BaseActiivity
import com.sk.waste.app.config.AppConfig
import com.sk.waste.app.enums.EditEmum
import com.sk.waste.app.views.InterestDialog
import com.sk.waste.app.views.SkAlertDialog
import java.lang.StringBuilder
import java.util.Arrays

/**
 *  @time 2025/4/25  17:11
 *  @desc 兴趣标签
 */
class InterestActivityBackup : BaseActiivity() {

    @LCBindView(R.id.ac_interest_add_button)
    private lateinit var bttAdd: Button

    @LCBindView(R.id.ac_interest_recyclerview)
    private lateinit var recyclerView: RecyclerView

    private lateinit var adapter: InterestAdapter
    private val list = mutableListOf<String>()
    private var addDialog: InterestDialog? = null
    private var alertDialog: SkAlertDialog? = null
    private var pageState = EditEmum.ADD
    private var currentPosition = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_interest)
        super.onCreate(savedInstanceState)
        setBack()
        setTitleView("兴趣标签")
        initAdapter()
        notifyData()
        bttAdd.setOnClickListener {
            pageState = EditEmum.ADD
            showAddData("")
        }
    }

    private fun notifyData() {
        val interestStr = LCSPUtil[mContext, AppConfig.SP_USER_INTEREST].toString().trim()
        val array = interestStr.split(",")
        list.clear()
        for (item in array) {
            if (item.isNotEmpty()) {
                list.add(item)
            }
        }
        adapter.notifyDataSetChanged()
    }

    private fun initAdapter() {
        recyclerView.layoutManager = LinearLayoutManager(mContext)
//        adapter = InterestAdapter(mContext, list, mOnItemClickListener)
        recyclerView.adapter = adapter
    }

    private val mOnItemClickListener = object : LCInterface.LcCallBack {
        override fun callBack(vararg args: Any?) {
            val viewId = args[0] as Int
            currentPosition = args[1] as Int
            when (viewId) {
                R.id.item_interest_look -> {
                    pageState = EditEmum.LOOK
                }

                R.id.item_interest_edit -> {
                    pageState = EditEmum.EDIT
                    showAddData(list[currentPosition])
                }

                R.id.item_interest_delete -> {
                    pageState = EditEmum.DELETE
                    showAlert()
                }
            }
        }

    }

    /**
     *  @time 2025/5/9  17:53
     *  @desc 新增
     */
    private fun showAddData(content: String) {
        if (addDialog == null) {
            addDialog = InterestDialog(mContext)
            addDialog!!.setOnclickListener(mOnclickListener)
        }
        addDialog!!.setContent(content)
        addDialog!!.show()
    }

    private val mOnclickListener = object : LCInterface.LcOnPnButtonClickLitener {
        override fun onNegetiveClick(vararg args: Any?) {

        }

        override fun onPositiveClick(vararg args: Any?) {
            val lab = args[0] as String
            if (lab.isNotEmpty()) {
                if (pageState == EditEmum.EDIT) {
                    val positon = adapter.getCurrentSelect()
                    list[positon] = lab
                } else {
                    list.add(lab)
                }
                updateLocalData()
            }
        }
    }

    /**
     *  @time 2025/5/9  17:53
     *  @desc 新增
     */
    private fun showAlert() {
        if (alertDialog == null) {
            alertDialog = SkAlertDialog(mContext)
            alertDialog!!.setOnclickListener(mDeleteOnclickListener)
        }
        alertDialog!!.setTitleAndContent("删除提醒", "标签删除提醒，请确认是否要删除此条兴趣标签。")
        alertDialog!!.showExpend()
    }


    private val mDeleteOnclickListener = object : LCInterface.LcOnPnButtonClickLitener {
        override fun onNegetiveClick(vararg args: Any?) {
        }

        override fun onPositiveClick(vararg args: Any?) {
            list.removeAt(currentPosition)
            updateLocalData()
        }
    }

    private fun updateLocalData() {
        val buildStr = StringBuilder()
        for (index in 0 until list.size) {
            if (index != list.size - 1) {
                buildStr.append("${list[index]},")
            } else {
                buildStr.append(list[index])
            }
        }
        LCSPUtil.save(mContext, AppConfig.SP_USER_INTEREST, buildStr.toString())
        notifyData()
    }

}